package com.qjcc.bean;

import android.util.Log;
import android.util.Xml;

import com.qjcc.util.StringUtils;

import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

/**
 * 应用程序更新实体类
 * @version 1.0
 */
public class Update implements Serializable{

	public final static String UTF8 = "UTF-8";
	public final static String NODE_ROOT = "sctf";

	private int versionCode;
	private String versionName;
	private String downloadUrl;
	private String updateLog;
	private String forceUpdate;
	private String packageName;

	public String getPackageName() {
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public int getVersionCode() {
		return versionCode;
	}
	public void setVersionCode(int versionCode) {
		this.versionCode = versionCode;
	}
	public String getVersionName() {
		return versionName;
	}
	public void setVersionName(String versionName) {
		this.versionName = versionName;
	}
	public String getDownloadUrl() {
		return downloadUrl;
	}
	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}
	public String getUpdateLog() {
		return updateLog;
	}
	public void setUpdateLog(String updateLog) {
		this.updateLog = updateLog;
	}
	public String getForceUpdate() {
		return forceUpdate;
	}

	public void setForceUpdate(String forceUpdate) {
		this.forceUpdate = forceUpdate;
	}
	public static Update parse(InputStream inputStream) throws IOException{
		Update update = null;
		//获得XmlPullParser解析器
		XmlPullParser xmlParser = Xml.newPullParser();
		try {
			xmlParser.setInput(inputStream, UTF8);
			//获得解析到的事件类别，这里有开始文档，结束文档，开始标签，结束标签，文本等等事件。
			int evtType=xmlParser.getEventType();
			//一直循环，直到文档结束
			while(evtType!=XmlPullParser.END_DOCUMENT){
				String tag = xmlParser.getName();
				switch(evtType){
					case XmlPullParser.START_TAG:
						//通知信息
						if(tag.equalsIgnoreCase("sctf")){
							update = new Update();
						}else if(update != null){
							if(tag.equalsIgnoreCase("versionCode"))
							{
								update.setVersionCode(StringUtils.toInt(xmlParser.nextText(), 0));
							}
							else if(tag.equalsIgnoreCase("versionName"))
							{
								update.setVersionName(xmlParser.nextText());
							}
							else if(tag.equalsIgnoreCase("packageName"))
							{
								update.setPackageName(xmlParser.nextText());
							}
							else if(tag.equalsIgnoreCase("forceUpdate"))
							{
								update.setForceUpdate(xmlParser.nextText());
							}
							else if(tag.equalsIgnoreCase("downloadUrl"))
							{
								update.setDownloadUrl(xmlParser.nextText());
							}
							else if(tag.equalsIgnoreCase("updateLog"))
							{
								update.setUpdateLog(xmlParser.nextText());
							}

						}
						break;
					case XmlPullParser.END_TAG:
						break;
				}
				//如果xml没有结束，则导航到下一个节点
				evtType=xmlParser.next();
			}
			if(update.getPackageName() != null
					&& update.getVersionName() != null
					&& update.getDownloadUrl() != null){
				update.setDownloadUrl(update.getDownloadUrl()+update.getPackageName()+"_"+update.getVersionName()+".apk");
			}
		} catch (XmlPullParserException e) {
			Log.e("Update",e.getMessage());
		} finally {
			inputStream.close();
		}
		return update;
	}

	public static Update parseJsonObj(JSONObject data){
		Update update = null;
		try{
			update = new Update();
			update.setForceUpdate(data.getString("forceUpdate"));
			update.setDownloadUrl(data.getString("downloadUrl"));
			update.setPackageName(data.getString("packageName"));
			update.setUpdateLog(data.getString("updateLog"));
			update.setVersionName(data.getString("versionName"));
		}catch(Exception e){
			e.printStackTrace();
			update = null;
		}
		return update;
	}
}
